/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <rom.prevot@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return R. Prevot
 * ----------------------------------------------------------------------------
 */

/******************************************************************************\
 *          ____                _______    _____ _             _              *
 *         |  _ \              |__   __|  / ____| |           | |             *
 *         | |_) | ___  _ __ _ __ | | ___| (___ | |_ ___   ___| | __          *
 *         |  _ < / _ \| '__| '_ \| |/ _ \\___ \| __/ _ \ / __| |/ /          *
 *         | |_) | (_) | |  | | | | | (_) |___) | || (_) | (__|   <           *
 *         |____/ \___/|_|  |_| |_|_|\___/_____/ \__\___/ \___|_|\_\          *
 *                                                                            *
\******************************************************************************/

package app.core;

import app.Tools;
import app.models.Categorie;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;

/**
 *
 * @author PREVROMA
 */
public class CategorieListModel extends DefaultListModel {

    List<Categorie> categories;

    public CategorieListModel() {
        super();
        categories = new ArrayList<Categorie>();
        this.addElement(new Categorie("Toutes les categories"));
    }

    /**
     * Add a category
     * Verify if the category exist
     * @param obj 
     */
    @Override
    public void addElement(Object obj) {
        if (obj instanceof Categorie) {
            Categorie cat = (Categorie) obj;
            if(categories.contains(obj)){
                return;
            }
            categories.add(cat);
            Tools.debug("Ajout de la catégorie: " + cat);
            obj = cat.getLibelle();
        } else {
            categories.add(new Categorie("Null"));
        }
        super.addElement(obj);
    }

    /**
     * @todo verifier que ça ne pause pas de problème.
     * Si on ajoute des objets autres que des catégories, l'index va se dédoubler?
     * @param index
     * @return 
     */
    @Override
    public Object elementAt(int index) {
        return categories.get(index);
    }

    @Override
    public boolean removeElement(Object obj) {
        if (obj instanceof Categorie) {
            Categorie cat = (Categorie) obj;
            categories.remove(obj);
            obj = cat.getLibelle();
        }
        return super.removeElement(obj);
    }
    
    /**
     * todo TEST!
     * @param oldObj
     * @param newObj 
     */
    public void updateElement(Object oldObj, Object newObj){
        int iMax = getSize();
        for(int i = 0; i < iMax; i++){
            Object current = elementAt(i);
            if(current.equals(oldObj))
                current = newObj;
        }
    }
    
    
    
    
}
